package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.OrdersDao;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.pojo.AddressBook;
import com.itheima.pojo.OrderDetail;
import com.itheima.pojo.Orders;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.AddressBookService;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements OrdersService {

    @Autowired
    private HttpSession session;

    @Autowired
    private ShoppingCartDao shoppingCartDao;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Override
    public void add(Orders orders) {
        Long userId = (Long) session.getAttribute("userId");

        //用户id
        orders.setUserId(userId);
        //下单时间
        orders.setOrderTime(LocalDateTime.now());
        //
        String replace = UUID.randomUUID().toString().replace("-", "*");
        //订单号
        orders.setNumber(replace);
        //用户名
        orders.setUserName("小花");

        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        //查询购物车信息
        List<ShoppingCart> shoppingCarts = shoppingCartDao.selectList(wrapper);

        BigDecimal total = new BigDecimal(0.0);
        for (ShoppingCart shoppingCart : shoppingCarts) {
            BigDecimal amount = shoppingCart.getAmount(); //单价
            Integer number = shoppingCart.getNumber(); //数量
            BigDecimal decimal = amount.multiply(new BigDecimal(number));
            total = total.add(decimal);
        }
        //实收金额
        orders.setAmount(total);

        //根据地址id查询地址对象
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookService.getById(addressBookId);
        orders.setPhone(addressBook.getPhone());
        orders.setConsignee(addressBook.getConsignee());
        orders.setAddress(addressBook.getDetail());
        orders.setCheckoutTime(LocalDateTime.now());
        save(orders);

        //2.1 保存订单明细
        for (ShoppingCart shoppingCart : shoppingCarts) {
            //一个购物车数据，转化为订单明细
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setOrderId(orders.getId());
            orderDetail.setAmount(shoppingCart.getAmount());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetailService.save(orderDetail);
        }
        //3.清空购物车
        shoppingCartDao.delete(wrapper);
    }

}